package com.restfinder.user.managedbean;

import static com.restfinder.common.component.FacesUtils.*;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import lombok.Setter;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import javax.faces.bean.ViewScoped;

import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.springframework.faces.webflow.JsfUtils;

import com.restfinder.common.component.FacesUtils;
import com.restfinder.common.component.PropertiesUtils;
import com.restfinder.managedbean.StandardManagedBean;
import com.restfinder.user.model.User;
import com.restfinder.user.service.UserService;

@ManagedBean(name="registerMB")
@ViewScoped
@Slf4j
public class RegisterManagedBean implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@ManagedProperty(name="userService", value="#{userService}")
	@Getter @Setter
	private UserService userService;
	
	@Getter @Setter
	private User user;	
	@Getter @Setter
	private String confirmPassword;
	
	@PostConstruct
	public void init(){
		user = new User();
	}
	
	public String save(){
		try {
			user.setAvatarSrc(PropertiesUtils.getPropertyValue("user_avatar_src", ""));
			userService.register(user);
			putIntoFlashScope("USER_REGISTERED", "X");
			return "SUCCESS";
		} catch (Exception e) {
			log.error("Exception while registrating user",e);
			e.printStackTrace();
			addErrorMessage("Cannot register user.", e.getMessage());
			return "FAILURE";
		}
	}
	
}
